package ide.ccf.ccproject.wizards.pages;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ide.ccf.ccproject.ProjectPlugin;
import ide.ccf.ccproject.common.WizardUtils;
import ide.ccf.ccproject.dialogs.WizardNewProjectCreationPage;
import ide.ccf.ccproject.managedbuilder.AuthorityUI;
import ide.ccf.ccproject.managedbuilder.CCFConfigWizardPage;
import ide.ccf.ccproject.managedbuilder.ConfigConstrants;
import ide.ccf.ccproject.managedbuilder.MbsCustomPageManager;
import ide.ccf.ccproject.managedbuilder.MbsWizardHandler;
import ide.ccf.ccproject.preferences.CCFPreferencePage;
import ide.ccf.ccproject.preferences.PreferenceConstants;
import ide.ccf.ccproject.translate.Message;

import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
import org.eclipse.cdt.managedbuilder.core.IManagedProject;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ContainerSelectionDialog;

public class MetadataWizardPage extends WizardPage {
	public static final String PAGE_ID = "ide.ccf.ccproject.wizards.pages.MetadataWizardPage"; 
	private ISelection selection;
	private Group filegroup;
	private static Text containerText;
	private static Combo c_sdk;
	private static Combo c_runtime;
	private static Text tName;
	private static Text tVersion;
	private static Text tCommand;
	private Table table_per;
	private static String strShared = "";
	private static String strSockets = "";
	private static String strFilesystems = "";
	private boolean bWayland = false, bX11 = false;
	private static AuthorityUI ai;
	private MbsWizardHandler handler;
	private String projName = null;
	public MetadataWizardPage(ISelection selection) {
		super("wizardPage");
		setTitle(Message.MetadataWizardPage_1);
		setDescription(Message.MetadataWizardPage_2);
		this.selection = selection;
		ai = null;
		handler = null;
	}
	
	public MetadataWizardPage(MbsWizardHandler mbsWizardHandler, WizardNewProjectCreationPage page) {
		super("wizardPage");
		setTitle(Message.MetadataWizardPage_1);
		setDescription(Message.MetadataWizardPage_2);
		setWizard(mbsWizardHandler.getWizard());
		handler = mbsWizardHandler;
		projName = page.getProjectName();
		ai = null;
	}

	public void createControl(Composite parent) {
		final ScrolledComposite scrolledComposite = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);  
		
		 final Composite mainComposite = new Composite(scrolledComposite, SWT.NONE);  
		 GridLayout layout = new GridLayout();
		 layout.numColumns = 1;
		 mainComposite.setLayout(layout);
		 mainComposite.setSize(500, 600);  
	     scrolledComposite.setContent(mainComposite); 
	     scrolledComposite.setMinSize(500, 900);
	     mainComposite.setVisible(true);

		
		Composite container = new Composite(mainComposite, SWT.NONE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		container.setLayoutData(gd);
		layout = new GridLayout();
		layout.numColumns = 3;
		layout.verticalSpacing = 9;
		container.setLayout(layout);
	
		if(handler == null)
		{
			Label label = new Label(container, SWT.NULL);
			label.setText(Message.UiFileWizardPage_10);

			containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
			gd = new GridData(GridData.FILL_HORIZONTAL);
			containerText.setLayoutData(gd);
			containerText.addModifyListener(new ModifyListener() 
			{
				public void modifyText(ModifyEvent e) 
				{
					dialogChanged();
				}
			});

			Button button = new Button(container, SWT.PUSH);
			button.setText(Message.UiFileWizardPage_11);
			button.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					handleBrowse();
				}
			});
		}
	
		filegroup = new Group(mainComposite, SWT.CHECK);
		filegroup.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
		filegroup.setText(Message.UiFileWizardPage_12);

		layout = new GridLayout();
		layout.numColumns = 2;
		filegroup.setLayout(layout);
		
		Label appName = new Label(filegroup, SWT.NULL);
		appName.setText("软件名称：");
		tName = new Text(filegroup, SWT.BORDER);
		tName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		tName.setFont(filegroup.getFont());
		if(projName != null && !projName.isEmpty())
			tName.setText(projName);
		Label appVersion = new Label(filegroup, SWT.NULL);
		appVersion.setText("软件版本：");
		tVersion = new Text(filegroup, SWT.BORDER);
		tVersion.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		tVersion.setFont(filegroup.getFont());
		tVersion.setText("1.0.0");
		
		Label appCommand = new Label(filegroup, SWT.NULL);
		appCommand.setText("软件执行命令：");
		tCommand = new Text(filegroup, SWT.BORDER);
		tCommand.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		tCommand.setFont(filegroup.getFont());
		if(projName != null && !projName.isEmpty())
			tCommand.setText(projName);
		
		Label l_1 = new Label(filegroup, SWT.NONE);
		l_1.setText(Message.CCProjectWizardOnePage_1);
		l_1.setFont(filegroup.getFont());
		l_1.setLayoutData(new GridData(GridData.BEGINNING));	
		c_sdk = new Combo(filegroup, SWT.DROP_DOWN | SWT.READ_ONLY);
		c_sdk.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		c_sdk.setFont(filegroup.getFont());
		Label l_2 = new Label(filegroup, SWT.NONE);
		l_2.setText(Message.CCProjectWizardOnePage_2);
		l_2.setFont(filegroup.getFont());
		l_2.setLayoutData(new GridData(GridData.BEGINNING));
		c_runtime = new Combo(filegroup, SWT.DROP_DOWN | SWT.READ_ONLY);
		c_runtime.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		c_runtime.setFont(filegroup.getFont());
		
		String[] strRuntimeVersions = getCCFRuntimeVersions();
		String[] strSdkVersions = getCCFSdkVersions();
		if(strRuntimeVersions != null)
		{
			c_runtime.setItems(strRuntimeVersions);
			for(int i = 0; i < strRuntimeVersions.length; i++)
				c_runtime.setData(strRuntimeVersions[i]);
		}
			
		if(strSdkVersions != null)
		{
			c_sdk.setItems(strSdkVersions);
			for(int j = 0; j < strSdkVersions.length; j++)
				c_sdk.setData(strSdkVersions[j]);
		}
			
		c_sdk.setText(CCFPreferencePage.getCCFSdkVersion() == null ? "" : CCFPreferencePage.getCCFSdkVersion() );
		c_runtime.setText(CCFPreferencePage.getCCFRuntimeVersion() == null ? "" : CCFPreferencePage.getCCFRuntimeVersion());

		{
			Composite c_per = new Composite(mainComposite, SWT.NONE);
			gd = new GridData(GridData.FILL_HORIZONTAL);
			gd.horizontalSpan = 2;
			c_per.setLayoutData(gd);
			c_per.setLayout(new FillLayout());
			Label lA = new Label(c_per, SWT.NONE);
			lA.setText(Message.CCFConfigWizardPage_13);
			
			Composite c_Per = new Composite(mainComposite, SWT.NONE);
			c_Per.setLayout(new GridLayout(2, false));
			c_Per.setLayoutData(new GridData(GridData.FILL_BOTH));
					
			ai = new AuthorityUI();
			ai.createAuthoritiesUI(c_Per);
			
			Composite c_ = new Composite(c_Per, SWT.NONE);
			c_.setLayoutData(new GridData(GridData.FILL_VERTICAL));
			c_.setLayout(new GridLayout());

			Button b_1 = new Button(c_, SWT.PUSH);
			b_1.setText(Message.CCFConfigWizardPage_4);
			b_1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			b_1.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					ai.setAllChecked(true);
					update();
				}});

			Button b_2 = new Button(c_, SWT.PUSH);
			b_2.setText(Message.CCFConfigWizardPage_5);
			b_2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			b_2.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					ai.setAllChecked(false);
					update();
				}});

			IPreferenceStore store = ProjectPlugin.getDefault().getPreferenceStore();
			String[] strAuthorities = store.getString(ConfigConstrants.CONTEXTALL).split(";");
			ai.setAuthoritiesUI(strAuthorities);
			ai.setCheckedElements();
			
			// 虚拟的占位符
			new Label(c_, 0).setLayoutData(new GridData(GridData.FILL_BOTH));

			Button b_3 = new Button(c_, SWT.PUSH);
			b_3.setText(Message.CCFConfigWizardPage_12);
			b_3.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			b_3.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					ai.setDefaultValue();
					ai.setCheckedElements();
				}});
		}
		initialize();
		if(handler == null)
			dialogChanged();
		
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		setControl(scrolledComposite);
		setPageComplete(true);
	}
	
	@SuppressWarnings("unused")
	private void createAuthoritiesUI(Composite c_Per) {
		Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
		table_per= new Table(c_Per, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
		GridData gridData = new GridData(GridData.FILL_BOTH);
		gridData.minimumHeight = 200;
		gridData.heightHint = 0;
		table_per.setLayoutData(gridData);
		table_per.setLayout(new GridLayout(3, false));
		table_per.setBackground(color);
		Label name = new Label(table_per, SWT.NULL);
		name.setBackground(color);
		name.setText("访问权限会话套接字列表");
		
		Label l_1 = new Label(table_per, SWT.NULL);
		Label l_2 = new Label(table_per, SWT.NULL);
		
		Button b_network = new Button(table_per, SWT.CHECK | SWT.LEFT);
		b_network.setText("与主机系统共享网络命名空间");
		b_network.setData("network");
		b_network.setBackground(color);
		b_network.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		Button b_ipc = new Button(table_per, SWT.CHECK | SWT.LEFT);
		b_ipc.setText(" 与主机系统共享进程通讯命名空间");
		b_ipc.setData("ipc");
		b_ipc.setBackground(color);
		b_ipc.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		Button b_x11 = new Button(table_per, SWT.CHECK | SWT.LEFT);
		b_x11.setText("使用X11显示窗口");
		b_x11.setData("x11");
		b_x11.setBackground(color);
		b_x11.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		Button b_wayland = new Button(table_per, SWT.CHECK | SWT.LEFT);
		b_wayland.setText("使用Wayland显示窗口");
		b_wayland.setData("wayland");
		b_wayland.setBackground(color);
		
		Button b_pulseaudio = new Button(table_per, SWT.CHECK | SWT.LEFT);
		b_pulseaudio.setText("使用PulseAudio播放声音");
		b_pulseaudio.setData("pulseaudio");
		b_pulseaudio.setBackground(color);
		
		Button b_sessionBus = new Button(table_per, SWT.CHECK | SWT.LEFT);
		b_sessionBus.setText("挂载到Session Bus");
		b_sessionBus.setData("session-bus");
		b_sessionBus.setBackground(color);
		
		Button b_systemBus = new Button(table_per, SWT.CHECK | SWT.LEFT);
		b_systemBus.setText("挂载到System Bus");
		b_systemBus.setData("system-bus");
		b_systemBus.setBackground(color);
		
		Label l_3 = new Label(table_per, SWT.NULL);
		Label l_4 = new Label(table_per, SWT.NULL);
		Label l_5= new Label(table_per, SWT.NULL);
		Label l_6 = new Label(table_per, SWT.NULL);
		Label l_7= new Label(table_per, SWT.NULL);
		
		Label nameF = new Label(table_per, SWT.NULL);
		nameF.setBackground(color);
		nameF.setText("可授予访问权限的主机系统文件");
		
		Label l_8 = new Label(table_per, SWT.NULL);
		Label l_9 = new Label(table_per, SWT.NULL);
		
		
		Button b_host = new Button(table_per, SWT.CHECK | SWT.LEFT);
		b_host.setText("允许访问所有主机文件");
		b_host.setData("host");
		b_host.setBackground(color);
		
		Composite group_1 = new Composite(table_per, SWT.NONE);
		group_1.setLayoutData(new GridData(GridData.FILL_BOTH));
		group_1.setLayout(new GridLayout(2, false));
		group_1.setBackground(color);
		
		Button b_w_host = new Button(group_1, SWT.RADIO | SWT.LEFT);
		b_w_host.setText("可写");
		b_w_host.setSelection(true);
		b_w_host.setBackground(color);
		
		Button b_ro_host = new Button(group_1, SWT.RADIO | SWT.LEFT);
		b_ro_host.setText("只读");
		b_ro_host.setBackground(color);
		
		Label l_10 = new Label(table_per, SWT.NULL);
		
		Button b_home = new Button(table_per, SWT.CHECK | SWT.LEFT);
		b_home.setText("允许完全访问用户主目录");
		b_home.setData("home");
		b_home.setBackground(color);
		
		Composite group_2 = new Composite(table_per, SWT.NONE);
		group_2.setLayoutData(new GridData(GridData.FILL_BOTH));
		group_2.setLayout(new GridLayout(2, false));
		group_2.setBackground(color);
		
		Button b_w_home = new Button(group_2, SWT.RADIO | SWT.LEFT);
		b_w_home.setText("可写");
		b_w_home.setSelection(true);
		b_w_home.setBackground(color);
		
		Button b_ro_home = new Button(group_2, SWT.RADIO | SWT.LEFT);
		b_ro_home.setText("只读");
		b_ro_home.setBackground(color);
		
		Label l_11 = new Label(table_per, SWT.NULL);
		
		Button b_xdgdesktop = new Button(table_per, SWT.CHECK | SWT.LEFT);
		b_xdgdesktop.setText("允许访问用户桌面目录");
		b_xdgdesktop.setData("xdg-desktop");
		b_xdgdesktop.setBackground(color);
		
		Composite group_3 = new Composite(table_per, SWT.NONE);
		group_3.setLayoutData(new GridData(GridData.FILL_BOTH));
		group_3.setLayout(new GridLayout(2, false));
		group_3.setBackground(color);
		
		Button b_w_xdgdesktop = new Button(group_3, SWT.RADIO | SWT.LEFT);
		b_w_xdgdesktop.setText("可写");
		b_w_xdgdesktop.setSelection(true);
		b_w_xdgdesktop.setBackground(color);
		
		Button b_ro_xdgdesktop = new Button(group_3, SWT.RADIO | SWT.LEFT);
		b_ro_xdgdesktop.setText("只读");
		b_ro_xdgdesktop.setBackground(color);
		
		Label l_12 = new Label(table_per, SWT.NULL);
		
		Button b_xdgdocuments = new Button(table_per, SWT.CHECK | SWT.LEFT);
		b_xdgdocuments.setText("允许访问用户文档目录");
		b_xdgdocuments.setData("xdg-documents");
		b_xdgdocuments.setBackground(color);
		
		Composite group_4 = new Composite(table_per, SWT.NONE);
		group_4.setLayoutData(new GridData(GridData.FILL_BOTH));
		group_4.setLayout(new GridLayout(2, false));
		group_4.setBackground(color);
		
		Button b_w_xdgdocuments = new Button(group_4, SWT.RADIO | SWT.LEFT);
		b_w_xdgdocuments.setText("可写");
		b_w_xdgdocuments.setSelection(true);
		b_w_xdgdocuments.setBackground(color);
		
		Button b_ro_xdgdocuments = new Button(group_4, SWT.RADIO | SWT.LEFT);
		b_ro_xdgdocuments.setText("只读");
		b_ro_xdgdocuments.setBackground(color);
		
		Label l_13 = new Label(table_per, SWT.NULL);
		
		Button b_xdgdownload = new Button(table_per, SWT.CHECK | SWT.LEFT);
		b_xdgdownload.setText("允许访问用户下载目录");
		b_xdgdownload.setData("xdg-download");
		b_xdgdownload.setBackground(color);
		
		Composite group_5 = new Composite(table_per, SWT.NONE);
		group_5.setLayoutData(new GridData(GridData.FILL_BOTH));
		group_5.setLayout(new GridLayout(2, false));
		group_5.setBackground(color);
		
		Button b_w_xdgdownload = new Button(group_5, SWT.RADIO | SWT.LEFT);
		b_w_xdgdownload.setText("可写");
		b_w_xdgdownload.setSelection(true);
		b_w_xdgdownload.setBackground(color);
		
		Button b_ro_xdgdownload = new Button(group_5, SWT.RADIO | SWT.LEFT);
		b_ro_xdgdownload.setText("只读");
		b_ro_xdgdownload.setBackground(color);
		
		Label l_14 = new Label(table_per, SWT.NULL);
		
		Button b_xdgmusic = new Button(table_per, SWT.CHECK | SWT.LEFT);
		b_xdgmusic.setText("允许访问用户音乐目录");
		b_xdgmusic.setData("xdg-music");
		b_xdgmusic.setBackground(color);
		
		Composite group_6 = new Composite(table_per, SWT.NONE);
		group_6.setLayoutData(new GridData(GridData.FILL_BOTH));
		group_6.setLayout(new GridLayout(2, false));
		group_6.setBackground(color);
		
		Button b_w_xdgmusic = new Button(group_6, SWT.RADIO | SWT.LEFT);
		b_w_xdgmusic.setText("可写");
		b_w_xdgmusic.setSelection(true);
		b_w_xdgmusic.setBackground(color);
		
		Button b_ro_xdgmusic = new Button(group_6, SWT.RADIO | SWT.LEFT);
		b_ro_xdgmusic.setText("只读");
		b_ro_xdgmusic.setBackground(color);
		
		Label l_15 = new Label(table_per, SWT.NULL);
		
		Button b_xdgpictures = new Button(table_per, SWT.CHECK | SWT.LEFT);
		b_xdgpictures.setText("允许访问用户图片目录");
		b_xdgpictures.setData("xdg-pictures");
		b_xdgpictures.setBackground(color);
		
		Composite group_7 = new Composite(table_per, SWT.NONE);
		group_7.setLayoutData(new GridData(GridData.FILL_BOTH));
		group_7.setLayout(new GridLayout(2, false));
		group_7.setBackground(color);
		
		Button b_w_xdgpictures = new Button(group_7, SWT.RADIO | SWT.LEFT);
		b_w_xdgpictures.setText("可写");
		b_w_xdgpictures.setSelection(true);
		b_w_xdgpictures.setBackground(color);
		
		Button b_ro_xdgpictures = new Button(group_7, SWT.RADIO | SWT.LEFT);
		b_ro_xdgpictures.setText("只读");
		b_ro_xdgpictures.setBackground(color);
		
		Label l_16 = new Label(table_per, SWT.NULL);
		
		Button b_xdgpublicshare = new Button(table_per, SWT.CHECK | SWT.LEFT);
		b_xdgpublicshare.setText("允许访问用户共享目录");
		b_xdgpublicshare.setData("xdg-public-share");
		b_xdgpublicshare.setBackground(color);
		
		Composite group_8 = new Composite(table_per, SWT.NONE);
		group_8.setLayoutData(new GridData(GridData.FILL_BOTH));
		group_8.setLayout(new GridLayout(2, false));
		group_8.setBackground(color);
		
		Button b_w_xdgpublicshare = new Button(group_8, SWT.RADIO | SWT.LEFT);
		b_w_xdgpublicshare.setText("可写");
		b_w_xdgpublicshare.setSelection(true);
		b_w_xdgpublicshare.setBackground(color);
		
		Button b_ro_xdgpublicshare = new Button(group_8, SWT.RADIO | SWT.LEFT);
		b_ro_xdgpublicshare.setText("只读");
		b_ro_xdgpublicshare.setBackground(color);
		
		Label l_17 = new Label(table_per, SWT.NULL);
		
		Button b_xdgtemplates = new Button(table_per, SWT.CHECK | SWT.LEFT);
		b_xdgtemplates.setText("允许访问用户模板目录");
		b_xdgtemplates.setData("xdg-templates");
		b_xdgtemplates.setBackground(color);
		
		Composite group_9 = new Composite(table_per, SWT.NONE);
		group_9.setLayoutData(new GridData(GridData.FILL_BOTH));
		group_9.setLayout(new GridLayout(2, false));
		group_9.setBackground(color);
		
		Button b_w_xdgtemplates = new Button(group_9, SWT.RADIO | SWT.LEFT);
		b_w_xdgtemplates.setText("可写");
		b_w_xdgtemplates.setSelection(true);
		b_w_xdgtemplates.setBackground(color);
		
		Button b_ro_xdgtemplates = new Button(group_9, SWT.RADIO | SWT.LEFT);
		b_ro_xdgtemplates.setText("只读");
		b_ro_xdgtemplates.setBackground(color);
		
		Label l_18 = new Label(table_per, SWT.NULL);
		
		Button b_xdgvideos = new Button(table_per, SWT.CHECK | SWT.LEFT);
		b_xdgvideos.setText("允许访问用户视频目录");
		b_xdgvideos.setData("xdg-videos");
		b_xdgvideos.setBackground(color);
		
		Composite group_10 = new Composite(table_per, SWT.NONE);
		group_10.setLayoutData(new GridData(GridData.FILL_BOTH));
		group_10.setLayout(new GridLayout(2, false));
		group_10.setBackground(color);
		
		Button b_w_xdgvideos = new Button(group_10, SWT.RADIO | SWT.LEFT);
		b_w_xdgvideos.setText("可写");
		b_w_xdgvideos.setSelection(true);
		b_w_xdgvideos.setBackground(color);
		
		Button b_ro_xdgvideos = new Button(group_10, SWT.RADIO | SWT.LEFT);
		b_ro_xdgvideos.setText("只读");
		b_ro_xdgvideos.setBackground(color);
		
		Label l_19 = new Label(table_per, SWT.NULL);
		
/*		Button b_xdgrun = new Button(table_per, SWT.CHECK | SWT.LEFT);
		b_xdgrun.setText("xdg-run");
		b_xdgrun.setData("xdg-run");
		b_xdgrun.setBackground(color);
		
		Composite group_11 = new Composite(table_per, SWT.NONE);
		group_11.setLayoutData(new GridData(GridData.FILL_BOTH));
		group_11.setLayout(new GridLayout(2, false));
		group_11.setBackground(color);
		
		Button b_w_xdgrun = new Button(group_11, SWT.RADIO | SWT.LEFT);
		b_w_xdgrun.setText("可写");
		b_w_xdgrun.setSelection(true);
		b_w_xdgrun.setBackground(color);
		
		Button b_ro_xdgrun = new Button(group_11, SWT.RADIO | SWT.LEFT);
		b_ro_xdgrun.setText("只读");
		b_ro_xdgrun.setBackground(color);
		
		Label l_20 = new Label(table_per, SWT.NULL);	*/
		
		final Button b_absolutepath = new Button(table_per, SWT.CHECK | SWT.LEFT);
		b_absolutepath.setText("允许访问绝对目录/相对目录");
		b_absolutepath.setData("path");
		b_absolutepath.setBackground(color);
		
		
		final Text t_path  = new Text(table_per, SWT.BORDER);
		t_path.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		t_path.setText("");
		t_path.setEnabled(false);
		
		b_absolutepath.addSelectionListener(new SelectionListener(){

			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			public void widgetSelected(SelectionEvent e) {
				if(b_absolutepath.getSelection())
					t_path.setEnabled(true);
				else
				{
					t_path.setText("");
					t_path.setEnabled(false);
				}			
		}
		});
		
		Composite group_12 = new Composite(table_per, SWT.NONE);
		group_12.setLayoutData(new GridData(GridData.FILL_BOTH));
		group_12.setLayout(new GridLayout(2, false));
		group_12.setBackground(color);
		
		Button b_w_absolutepath = new Button(group_12, SWT.RADIO | SWT.LEFT);
		b_w_absolutepath.setText("可写");
		b_w_absolutepath.setSelection(true);
		b_w_absolutepath.setBackground(color);
		
		Button b_ro_absolutepath = new Button(group_12, SWT.RADIO | SWT.LEFT);
		b_ro_absolutepath.setText("只读");
		b_ro_absolutepath.setBackground(color);
	}

	public static void createFile(IProgressMonitor monitor) throws CoreException {
		String filePath = getContainerName() + "/" + "metadata";
		if(ai != null)
		{
			String out = ai.getCheckedElements();
			IPreferenceStore store = ProjectPlugin.getDefault().getPreferenceStore();
			store.setValue(ConfigConstrants.CONTEXTALL, out);
			setAuthorities();
			String[] cBindings = {tName.getText().trim(), tVersion.getText().trim(), c_runtime.getText().trim(), c_sdk.getText().trim(), tCommand.getText().trim(), strShared, strSockets, strFilesystems, "true", "true"};
			String source = NLS.bind(Message.MetadataWizardPage_0, cBindings);
			IFile file = WizardUtils.createFile(new Path(filePath), monitor);
			file.create(new ByteArrayInputStream(source.getBytes()), true, monitor);
		}
	}
	

	private static void setAuthorities() {
		strShared = "";
		strSockets = "";
		strFilesystems = "";
		IPreferenceStore store = ProjectPlugin.getDefault().getPreferenceStore();
		String[] strAuthorities = store.getString(ConfigConstrants.CONTEXTALL).split(";");
		for(int i = 0; i < strAuthorities.length; i++)
		{
			strAuthorities[i] = strAuthorities[i].trim();
			if(strAuthorities[i].equals("network") || strAuthorities[i].equals("ipc"))
				strShared += strAuthorities[i] + ";";
			else if(strAuthorities[i].equals("x11") || strAuthorities[i].equals("wayland")
					|| strAuthorities[i].equals("pulseaudio") || strAuthorities[i].equals("session-bus")
					|| strAuthorities[i].equals("system-bus"))
				strSockets += strAuthorities[i] + ";";
			else if(strAuthorities[i].equals("host") || strAuthorities[i].equals("home")
					|| strAuthorities[i].equals("xdg-desktop") || strAuthorities[i].equals("xdg-documents")
					|| strAuthorities[i].equals("xdg-download") || strAuthorities[i].equals("xdg-music")
					|| strAuthorities[i].equals("xdg-pictures") || strAuthorities[i].equals("xdg-public-share")
					|| strAuthorities[i].equals("xdg-templates") || strAuthorities[i].equals("xdg-videos")
					|| strAuthorities[i].equals("xdg-run") || strAuthorities[i].equals("host:ro") || strAuthorities[i].equals("home:ro")
					|| strAuthorities[i].equals("xdg-desktop:ro") || strAuthorities[i].equals("xdg-documents:ro")
					|| strAuthorities[i].equals("xdg-download:ro") || strAuthorities[i].equals("xdg-music:ro")
					|| strAuthorities[i].equals("xdg-pictures:ro") || strAuthorities[i].equals("xdg-public-share:ro")
					|| strAuthorities[i].equals("xdg-templates:ro") || strAuthorities[i].equals("xdg-videos:ro")
					|| strAuthorities[i].equals("xdg-run:ro"))
				strFilesystems += strAuthorities[i] + ";";
		}
	}

	/**
	 * 对话框变化事件处理
	 */
	private void dialogChanged() 
	{
		IProject project = null;
		IResource container = ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(getContainerName()));
		if(container != null)
			project = container.getProject();
		if(project != null)
		{
			IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(project);
			IManagedProject mProj = info.getManagedProject();
			IConfiguration cfg = mProj.getConfigurations()[0];
			String s = cfg.getArtifactName();
			tCommand.setText((s == null || s.equals("${ProjName}")) ? "" : s);
		}

		if (getContainerName().length() == 0) 
		{
			updateStatus(Message.ResourceWizardPage_4);
			return;
		}
		
		if (container == null || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) 
		{
			updateStatus(Message.ResourceWizardPage_5);
			return;
		}
		
		if (!container.isAccessible()) 
		{
			updateStatus(Message.ResourceWizardPage_6);
			return;
		}
		
		updateStatus(null);
	}
	
	/**
	 * 更新状态
	 * @param message
	 */
	private void updateStatus(String message) 
	{
		setErrorMessage(message);
		setPageComplete(message == null);
	}

	/**
	 * 获取容器名称
	 * @return
	 */
	public static String getContainerName() 
	{
		if(containerText != null)
			return containerText.getText();
		else
			return null;
	}
	
	/**
	 * 初始化
	 */
	private void initialize() 
	{
		if (selection != null && selection.isEmpty() == false && selection instanceof IStructuredSelection) 
		{
			IStructuredSelection ssel = (IStructuredSelection) selection;
			
			if (ssel.size() > 1)
				return;
			
			Object obj = ssel.getFirstElement();
			
			if (!(obj instanceof IResource) && (obj instanceof IAdaptable))
				obj = ((IAdaptable)obj).getAdapter(IResource.class);
			
			if (obj instanceof IResource) 
			{
				IContainer container;
				
				if (obj instanceof IContainer)
					container = (IContainer) obj;
				else
					container = ((IResource) obj).getParent();
				
				containerText.setText(container.getFullPath().makeRelative().toString());
			}
		}
		tName.setFocus();
	}
	
	/**
	 * 浏览按钮事件处理
	 * 使用标准容器选择对话框选择源文件夹字段值
	 */
	private void handleBrowse() 
	{
		ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(), false, "选择源文件夹");
		
		if (dialog.open() == ContainerSelectionDialog.OK) 
		{
			Object[] result = dialog.getResult();
			
			if (result.length == 1) 
			{
				containerText.setText(((Path) result[0]).makeRelative().toString());
			}
		}
	}
	
	public static String[] getCCFSdkVersions() {
		if(ProjectPlugin.getDefault() != null)
		{
			IPreferenceStore store = ProjectPlugin.getDefault().getPreferenceStore();
			
			if (!store.contains(PreferenceConstants.CCFSDK_VERSIONS))
				return null;
			
			String defaultVersions = store.getString(PreferenceConstants.CCFSDK_VERSIONS);
			defaultVersions = defaultVersions.substring(1, defaultVersions.length()-1);
			//System.out.println("defaultVersions:"+defaultVersions);
			//cut string
			String[] b = defaultVersions.split(",");
			if(b.length > 0)
				return b;	
		}
		return null;
	}

	public static String[] getCCFRuntimeVersions() {
		if(ProjectPlugin.getDefault() != null)
		{
			IPreferenceStore store = ProjectPlugin.getDefault().getPreferenceStore();
			
			if (!store.contains(PreferenceConstants.CCFRUNTIME_VERSIONS))
				return null;
			
			String defaultVersions = store.getString(PreferenceConstants.CCFRUNTIME_VERSIONS);
			defaultVersions = defaultVersions.substring(1, defaultVersions.length()-1);
			//System.out.println("defaultVersions:"+defaultVersions);
			//cut string
			String[] b = defaultVersions.split(",");
			if(b.length > 0)
				return b;	
		}
		return null;
	}
	
	protected void update() {
		getWizard().getContainer().updateButtons();
		getWizard().getContainer().updateMessage();
		getWizard().getContainer().updateTitleBar();
	}

	public static void createFile(IProgressMonitor monitor, IProject newProject) {
		// TODO Auto-generated method stub
		String filePath = newProject.getLocation().toString() + "/" + "metadata";
		if(ai != null)
		{
			String out = ai.getCheckedElements();
			IPreferenceStore store = ProjectPlugin.getDefault().getPreferenceStore();
			store.setValue(ConfigConstrants.CONTEXTALL, out);
			setAuthorities();
			String[] cBindings = {tName.getText().trim(), tVersion.getText().trim(), c_runtime.getText().trim(), c_sdk.getText().trim(), tCommand.getText().trim(), strShared, strSockets, strFilesystems, "true", "true"};
			String source = NLS.bind(Message.MetadataWizardPage_0, cBindings);
			IFile file;
			try {
				file = WizardUtils.createFile(new Path(filePath), monitor);
				file.create(new ByteArrayInputStream(source.getBytes()), true, monitor);
			} catch (CoreException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public IWizardPage getNextPage() {
		return MbsCustomPageManager.getNextPage(PAGE_ID);
	}
	
	public static String getAutorities(){
		if(ai != null)
			return ai.getCheckedElements();
		return null;
	}
}